package com.ky.serialport.sensor;


import com.ky.serialport.bean.ComBean;
import com.ky.serialport.utils.MyFunc;

import static com.ky.serialport.utils.RandomUtils.myRandom;

/**
 * 位移传感器数据包解析
 */

public class WeiYiDataBean {

    public Long time = 0L;
    public Float[] weiYiArray = new Float[10]; //位移数据
    public float weiYiVel = 0F; //均值
    public int power = 0; //电量
    public int signal = 0; //信号
    public int index = -1;
    public ComBean comBean;


    public WeiYiDataBean() {
    }

    public WeiYiDataBean(ComBean comBean ) {
        caculate(comBean.recData);
        time = comBean.recTime;
        this.comBean = comBean;
    }

    public WeiYiDataBean(byte[] recData) {
        caculate(recData);
        time = System.currentTimeMillis();
    }

    public WeiYiDataBean caculate(ComBean comBean){

        time = comBean.recTime;
        return caculate(comBean.recData);
    }

    public WeiYiDataBean caculate(byte[] buffer) {
        power = MyFunc.getPower(buffer[34],buffer[35]);
        signal = MyFunc.getSignal(buffer[36]); //测试信号
        index = buffer[10];

        weiYiArray[0] = (float)MyFunc.twoByte2IntNoSign(buffer[14],buffer[15])/100f;
        weiYiArray[1] = (float)MyFunc.twoByte2IntNoSign(buffer[16],buffer[17])/100f;
        weiYiArray[2] = (float)MyFunc.twoByte2IntNoSign(buffer[18],buffer[19])/100f;
        weiYiArray[3] = (float)MyFunc.twoByte2IntNoSign(buffer[20],buffer[21])/100f;
        weiYiArray[4] = (float)MyFunc.twoByte2IntNoSign(buffer[22],buffer[23])/100f;
        weiYiArray[5] = (float)MyFunc.twoByte2IntNoSign(buffer[24],buffer[25])/100f;
        weiYiArray[6] = (float)MyFunc.twoByte2IntNoSign(buffer[26],buffer[27])/100f;
        weiYiArray[7] = (float)MyFunc.twoByte2IntNoSign(buffer[28],buffer[29])/100f;
        weiYiArray[8] = (float)MyFunc.twoByte2IntNoSign(buffer[30],buffer[31])/100f;
        weiYiArray[9] = (float)MyFunc.twoByte2IntNoSign(buffer[32],buffer[33])/100f;

        float allVel = 0f;
        for (int i = 0; i <10; i++) {
            allVel += weiYiArray[i];
        }
        weiYiVel = allVel/10f;
        return this;
    }

    public WeiYiDataBean test() {
        time = System.currentTimeMillis();
        signal = 100+ myRandom.nextInt(100);
        return this;
    }

    @Override
    public String toString() {
        return "WeiYiDataBean{" +
                "time=" + time +
                ", weiYiVel=" + weiYiVel +
                ", power=" + power +
                ", signal=" + signal +
                '}';
    }
}
